package com.easytake.emotioncat.config;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * v3/api-docs
 * doc.html
 */
@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI api() {
        return new OpenAPI()
                .info(new Info().title("情绪猫接口文档")
                        .description("Emotion Cat API Doc")
                        .version("1.0")
                        .summary("summary")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org"))
                        .contact(new Contact().name("BJ开发小分队").email("chro008lxm@gmail.com")))
                .externalDocs(new ExternalDocumentation()
                        .description("SpringShop Wiki Documentation")
                        .url("https://springshop.wiki.github.org/docs"));
    }

    @Bean
    public GroupedOpenApi userApi() {
        String[] paths = {"/**"};
        String[] packagedToMatch = {"com.easytake.emotioncat"};
        return GroupedOpenApi.builder().group("后端模块")
                .pathsToMatch(paths)
                .pathsToExclude("/health", "/health")
                .addOperationCustomizer((operation, handlerMethod) -> {
                    //生成swagger接口文档的时候把请求头需要wx_uid的地方都改成需要token, wx_uid是在拦截器中通过token转换的
                    List<Parameter> parameterList = operation.getParameters();
                    if (!CollectionUtils.isEmpty(parameterList)) {
                        List<Parameter> removeWxUidList = new ArrayList<>();
                        parameterList.forEach(parameter -> {
                            if (Objects.equals(parameter.getName(), "wx_uid")&&Objects.equals(parameter.getIn(), "header")) {
                                removeWxUidList.add(parameter);
                            }
                        });
                        if (removeWxUidList.size() > 0) {
                            parameterList.removeAll(removeWxUidList);
                            operation.addParametersItem(new HeaderParameter().name("token").required(true).example("5a23dgfjkls23jlsdflks").description("用户token")
                                    .schema(new StringSchema()._default(null).name("token").description("用户token")));
                        }
                    }
                    return operation;
                })
                .packagesToScan(packagedToMatch).build();
    }
}
